﻿using Demo.AttachFileService.Models;
using Microsoft.EntityFrameworkCore;
using System.Linq;
namespace Demo.AttachFileService.Services
{
    public class SqliteDbContext : DbContext
    {
        public SqliteDbContext(DbContextOptions<SqliteDbContext> options)
        : base(options)
        {
        }
        public SqliteDbContext() : base() { }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlite("Data Source=F:\\AttachFile\\attachfile.db");
        }

        public DbSet<AlarmInfo> AlarmInfos { get; set; }

        

        public DbSet<AttachFile> AttachFiles { get; set; }

        public DbSet<FileRecvRecored> FileRecvRecords { get; set; }


        public IQueryable<AlarmInfo> QueryAlarmInfos { get { return AlarmInfos; } }
        public IQueryable<AttachFile> QueryAttachFiles { get { return AttachFiles; } }
        public IQueryable<FileRecvRecored> QueryFileRecvRecords { get { return FileRecvRecords; } }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<AlarmInfo>(c =>
              {
                  c.HasKey(a => a.Id);
                  c.Property(e => e.AlarmId).HasMaxLength(32).IsRequired();
                  c.Property(e => e.AlarmNo).HasMaxLength(32).IsRequired();
                  c.Property(e => e.TerminalPhoneNo).HasMaxLength(12).IsRequired();
              });
            modelBuilder.Entity<AttachFile>(c =>
            {
                c.HasKey(a => a.Id);
                c.Property(e => e.AlarmId).HasMaxLength(32).IsRequired();
            });
            modelBuilder.Entity<FileRecvRecored>(c =>
            {
                c.HasKey(c=>c.Id);
                c.Ignore(e => e.Next);
                c.HasIndex(c=>c.FileName);

                c.Property(e => e.FileName).HasMaxLength(50).IsRequired();
                //c.Property(e => e.TerminalPhoneNo).HasMaxLength(12).IsRequired();
            });
        }
    }
     
}
